@page "/login"
@using BlazorWasmAuth.Identity
@inject IAccountManagement Acct

<PageTitle>Login</PageTitle>

<h1>Login</h1>

<AuthorizeView>
    <Authorized>
        <div class="alert alert-success">You're logged in as @context.User.Identity?.Name.</div>
    </Authorized>
    <NotAuthorized>
        @if (errors)
        {
            @foreach (var error in errorList)
            {
                <div class="alert alert-danger">@error</div>
            }
        }
        <div class="flex-outer">
            <div>
                <label for="email">
                    Email:
                </label>
                <input required id="email" name="emailInput" placeholder="Enter your email address" type="email" @bind-value="email" />
            </div>
            <div>
                <label for="password">
                    Password:
                </label>
                <input required id="password" name="passwordInput" placeholder="Enter your password" type="password" @bind-value="password" />
            </div>
            <div>
                <button class="btn btn-primary" @onclick="DoLoginAsync">Login</button>
            </div>
        </div>
    </NotAuthorized>
</AuthorizeView>

@code {
    private bool success, errors;
    private string email = string.Empty;
    private string password = string.Empty;
    private string[] errorList = [];

    public async Task DoLoginAsync()
    {
        success = errors = false;
        errorList = [];

        if (string.IsNullOrWhiteSpace(email))
        {
            errors = true;
            errorList = ["Email is required."];

            return;
        }

        if (string.IsNullOrWhiteSpace(password))
        {
            errors = true;
            errorList = ["Password is required."];

            return;
        }

        var result = await Acct.LoginAsync(email, password);

        if (result.Succeeded)
        {
            success = true;
            email = password = string.Empty;
        }
        else
        {
            errors = true;
            errorList = result.ErrorList;
        }
    }
}
